package com.example.springbootmybatis.web;

import com.example.springbootmybatis.entity.Area;
import com.example.springbootmybatis.service.AreaService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class AreaController {

    @Autowired
    private AreaService areaService;


    @GetMapping("list")
    private Map<String, Object> list(){
        List<Area> areas = areaService.findall();
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("areas", areas);
        return res;
    }
    @GetMapping("listbyid")
    private Map<String, Object> listbyid(int id){
        Area area = areaService.findById(id);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("area", area);
        return res;
    }
    @PostMapping("add")
    private Map<String, Object> add(@RequestBody Area o){
        boolean area = areaService.save(o);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("success", area);
        return res;
    }
    @PostMapping("upd")
    private Map<String, Object> upd(@RequestBody Area o){
        boolean area = areaService.edit(o);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("success", area);
        return res;
    }
    @GetMapping(value = "del")
    private Map<String, Object> del(int id){
        boolean area = areaService.del(id);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("success", area);
        return res;
    }


}
